﻿using Learun.Util;
using Learun.Util.SqlSugar;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using DbType = System.Data.DbType;

namespace Learun.Application.Base.SystemModule
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创建人：研发部
    /// 日 期：2017.03.08
    /// 描 述：数据库连接
    /// </summary>
    //TODO 更新连接串地址 this.BaseRepository(entity.F_DbConnection, entity.F_DbType)
    public class DatabaseLinkService
    {
        #region 仓储
        Repository<DatabaseLinkEntity> _databaseLinkRepository => new Repository<DatabaseLinkEntity>();
        #endregion
        #region 构造函数和属性
        private string fieldSql;
        public DatabaseLinkService()
        {
            fieldSql = @"
                    t.F_DatabaseLinkId,
                    t.F_ServerAddress,
                    t.F_DBName,
                    t.F_DBAlias,
                    t.F_DbType,
                    t.F_DbConnection,
                    t.F_DESEncrypt,
                    t.F_SortCode,
                    t.F_DeleteMark,
                    t.F_EnabledMark,
                    t.F_Description,
                    t.F_CreateDate,
                    t.F_CreateUserId,
                    t.F_CreateUserName,
                    t.F_ModifyDate,
                    t.F_ModifyUserId,
                    t.F_ModifyUserName
                ";
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取自定义查询条件（公共）
        /// </summary>
        /// <param name="moduleUrl">访问的功能链接地址</param>
        /// <param name="userId">用户ID（用户ID为null表示公共）</param>
        /// <returns></returns>
        public IEnumerable<DatabaseLinkEntity> GetList()
        {
            try
            {
                var strSql = new StringBuilder();
                strSql.Append("SELECT ");
                strSql.Append(fieldSql);
                strSql.Append(" FROM LR_Base_DatabaseLink t WHERE  t.F_DeleteMark = 0 ");
                //return this.BaseRepository().FindList<DatabaseLinkEntity>(strSql.ToString());
                return SqlSugarHelper.Db.SqlQueryable<DatabaseLinkEntity>(strSql.ToString()).ToList();
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 删除自定义查询条件
        /// </summary>
        /// <param name="keyValue">主键</param>
        public void VirtualDelete(string keyValue)
        {
            try
            {
                DatabaseLinkEntity entity = new DatabaseLinkEntity()
                {
                    F_DatabaseLinkId = keyValue,
                    F_DeleteMark = 1
                };
                _databaseLinkRepository.Update(entity);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        /// <summary>
        /// 保存自定义查询（新增、修改）
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <param name="departmentEntity">部门实体</param>
        /// <returns></returns>
        public bool SaveEntity(string keyValue, DatabaseLinkEntity databaseLinkEntity)
        {
            try
            {
                /*测试数据库连接串"******";*/
                if (!string.IsNullOrEmpty(keyValue) && databaseLinkEntity.F_DbConnection == "******")
                {
                    databaseLinkEntity.F_DbConnection = null;// 不更新连接串地址
                }
                else
                {
                    try
                    {
                        //databaseLinkEntity.F_ServerAddress = this.BaseRepository(databaseLinkEntity.F_DbConnection, databaseLinkEntity.F_DbType).getDbConnection().DataSource;
                    }
                    catch (Exception)
                    {
                        return false;
                    }
                }
                if (!string.IsNullOrEmpty(keyValue))
                {
                    databaseLinkEntity.Modify(keyValue);
                    _databaseLinkRepository.Update(databaseLinkEntity);
                }
                else
                {
                    databaseLinkEntity.Create();
                    _databaseLinkRepository.Insert(databaseLinkEntity);
                }
                return true;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        } 
        #endregion

        #region 扩展方法
        /// <summary>
        /// 测试数据数据库是否能连接成功
        /// </summary>
        /// <param name="connection">连接串</param>
        /// <param name="dbType">数据库类型</param>
        public bool TestConnection(string connection, string dbType)
        {
            try
            {
                SqlSugarHelper.Db.BeginTran();
                SqlSugarHelper.Db.CommitTran();
                return true;
            }
            catch (Exception)
            {
                SqlSugarHelper.Db.RollbackTran();
                return false;
            }
        }
        /// <summary>
        /// 根据指定数据库执行sql语句
        /// </summary>
        /// <param name="entity">数据库实体</param>
        /// <param name="sql">sql语句</param>
        /// <param name="dbParameter">参数</param>
        public void ExecuteBySql(DatabaseLinkEntity entity, string sql, object dbParameter)
        {
            try
            {
                if (dbParameter is JObject)
                {
                    //var paramter = SqlHelper.JObjectToParameter((JObject)dbParameter);
                    var paramter = new List<SugarParameter>();
                    foreach (var item in (JObject)dbParameter)
                    {
                        paramter.Add(new SugarParameter(item.Key.ToString(), item.Value.ToString()));
                    }
                    if (entity.F_DatabaseLinkId == "systemdb")
                    {
                         SqlSugarHelper.Db.Ado.ExecuteCommand(sql, paramter);
                    }
                    else
                    {
                        //this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteBySql(sql, paramter);
                    }
                }
                else if (dbParameter is List<FieldValueParam>)
                {
                    //var paramter = SqlHelper.FieldValueParamToParameter((List<FieldValueParam>)dbParameter);
                    var paramter = new List<SugarParameter>();
                    foreach (var item in (List<FieldValueParam>)dbParameter)
                    {
                        paramter.Add(new SugarParameter(item.name, item.value, (DbType)item.type));
                    }
                    if (entity.F_DatabaseLinkId == "systemdb")
                    {
                        SqlSugarHelper.Db.Ado.ExecuteCommand(sql, paramter);
                    }
                    else
                    {
                        //this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteBySql(sql, paramter);
                    }
                }
                else
                {
                    if (entity.F_DatabaseLinkId == "systemdb")
                    {
                        SqlSugarHelper.Db.Ado.ExecuteCommand(sql, dbParameter);
                    }
                    else
                    {
                        //this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteBySql(sql, dbParameter);
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 根据指定数据库执行sql语句
        /// </summary>
        /// <param name="entity">数据库实体</param>
        /// <param name="procName">存储过程</param>
        /// <param name="dbParameter">参数</param>
        public void ExecuteByProc(DatabaseLinkEntity entity, string procName, object dbParameter)
        {
            try
            {
                if (dbParameter is JObject)
                {
                    //var paramter = SqlHelper.JObjectToParameter((JObject)dbParameter);
                    var paramter = new List<SugarParameter>();
                    foreach (var item in (JObject)dbParameter)
                    {
                        paramter.Add(new SugarParameter(item.Key.ToString(), item.Value.ToString()));
                    }
                    if (entity.F_DatabaseLinkId == "systemdb")
                    {
                        //this.BaseRepository().ExecuteByProc(procName, paramter);
                        SqlSugarHelper.Db.Ado.UseStoredProcedure().ExecuteCommand(procName, paramter);
                    }
                    else
                    {
                        //this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteByProc(procName, paramter);
                    }
                }
                else if (dbParameter is List<FieldValueParam>)
                {
                    //var paramter = SqlHelper.FieldValueParamToParameter((List<FieldValueParam>)dbParameter);
                    var paramter = new List<SugarParameter>();
                    foreach (var item in (List<FieldValueParam>)dbParameter)
                    {
                        paramter.Add(new SugarParameter(item.name, item.value, (DbType)item.type));
                    }
                    if (entity.F_DatabaseLinkId == "systemdb")
                    {
                        //this.BaseRepository().ExecuteByProc(procName, paramter);
                        SqlSugarHelper.Db.Ado.UseStoredProcedure().ExecuteCommand(procName, paramter);
                    }
                    else
                    {
                        //this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteByProc(procName, paramter);
                    }
                }
                else
                {
                    if (entity.F_DatabaseLinkId == "systemdb")
                    {
                        var paramter = new List<SugarParameter>();
                        foreach (var item in (List<FieldValueParam>)dbParameter)
                        {
                            paramter.Add(new SugarParameter(item.name, item.value, (DbType)item.type));
                        }
                        SqlSugarHelper.Db.Ado.UseStoredProcedure().ExecuteCommand(procName, paramter);
                    }
                    else
                    {
                        //this.BaseRepository(entity.F_DbConnection, entity.F_DbType).ExecuteByProc(procName, dbParameter);
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        /// <summary>
        /// 根据数据库执行sql语句,查询数据->datatable
        /// </summary>
        /// <param name="entity">数据库实体</param>
        /// <param name="sql">sql语句</param>
        /// <param name="dbParameter">参数</param>
        /// <returns></returns>
        public DataTable FindTable(DatabaseLinkEntity entity, string sql, object dbParameter)
        {
            try
            {

                if (dbParameter is JObject)
                {
                    //if (entity.F_DatabaseLinkId == "systemdb")
                    //{
                    //    return this.BaseRepository().FindTable(sql, paramter);
                    //}
                    //else
                    //{
                    //    return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, paramter);
                    //}
                    var paramter = new List<SugarParameter>();
                    foreach (var item in (JObject)dbParameter)
                    {
                        paramter.Add(new SugarParameter(item.Key.ToString(), item.Value.ToString()));
                    }
                    return SqlSugarHelper.Db.Ado.GetDataTable(sql, paramter);
                }
                else if (dbParameter is List<FieldValueParam>)
                {
                    //var paramter = SqlHelper.FieldValueParamToParameter((List<FieldValueParam>)dbParameter);
                    //if (entity.F_DatabaseLinkId == "systemdb")
                    //{
                    //    return this.BaseRepository().FindTable(sql, paramter);
                    //}
                    //else
                    //{
                    //    return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, paramter);
                    //}
                    var paramter = new List<SugarParameter>();
                    foreach (var item in (List<FieldValueParam>)dbParameter)
                    {
                        paramter.Add(new SugarParameter(item.name, item.value, (DbType)item.type));
                    }
                    return SqlSugarHelper.Db.Ado.GetDataTable(sql, paramter);
                }
                else
                {
                    //if (entity.F_DatabaseLinkId == "systemdb")
                    //{
                    //    return this.BaseRepository().FindTable(sql, dbParameter);
                    //}
                    //else
                    //{
                    //    return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, dbParameter);
                    //}
                    return SqlSugarHelper.Db.Ado.GetDataTable(sql, dbParameter);
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        /// <summary>
        /// 根据数据库执行sql语句,查询数据->datatable（分页）
        /// </summary>
        /// <param name="entity">数据库实体</param>
        /// <param name="sql">sql语句</param>
        /// <param name="dbParameter">参数</param>
        /// <param name="pagination">分页参数</param>
        /// <returns></returns>
        public DataTable FindTable(DatabaseLinkEntity entity, string sql, object dbParameter, Pagination pagination) {
            try
            {

                if (dbParameter is JObject)
                {
                    //var paramter = SqlHelper.JObjectToParameter((JObject)dbParameter);
                    //if (entity.F_DatabaseLinkId == "systemdb")
                    //{
                    //    return this.BaseRepository().FindTable(sql, paramter, pagination);
                    //}
                    //else
                    //{
                    //    return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, paramter, pagination);
                    //}
                    var paramter = new List<SugarParameter>();
                    foreach (var item in (JObject)dbParameter)
                    {
                        paramter.Add(new SugarParameter(item.Key.ToString(), item.Value.ToString()));
                    }
                    return SqlSugarHelper.Db.Ado.GetDataTable(sql, paramter);
                }
                else if (dbParameter is List<FieldValueParam>)
                {
                    //var paramter = SqlHelper.FieldValueParamToParameter((List<FieldValueParam>)dbParameter);
                    //if (entity.F_DatabaseLinkId == "systemdb")
                    //{
                    //    return this.BaseRepository().FindTable(sql, paramter, pagination);
                    //}
                    //else
                    //{
                    //    return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, paramter, pagination);
                    //}
                    var paramter = new List<SugarParameter>();
                    foreach (var item in (List<FieldValueParam>)dbParameter)
                    {
                        paramter.Add(new SugarParameter(item.name, item.value, (DbType)item.type));
                    }
                    return SqlSugarHelper.Db.Ado.GetDataTable(sql, paramter);
                }
                else
                {
                    //if (entity.F_DatabaseLinkId == "systemdb")
                    //{
                    //    return this.BaseRepository().FindTable(sql, dbParameter, pagination);
                    //}
                    //else
                    //{
                    //    return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).FindTable(sql, dbParameter, pagination);
                    //}
                    return SqlSugarHelper.Db.Ado.GetDataTable(sql, dbParameter);
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #region 事务执行
        /// <summary>
        /// 开启事务
        /// </summary>
        /// <param name="entity">数据库连接信息</param>
        /// <returns></returns>
        //public IRepository BeginTrans(DatabaseLinkEntity entity)
        //{
        //    try
        //    {
        //        if (entity.F_DatabaseLinkId == "systemdb")
        //        {
        //            return this.BaseRepository().BeginTrans();
        //        }
        //        else
        //        {
        //            return this.BaseRepository(entity.F_DbConnection, entity.F_DbType).BeginTrans();
        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //        if (ex is ExceptionEx)
        //        {
        //            throw;
        //        }
        //        else
        //        {
        //            throw ExceptionEx.ThrowServiceException(ex);
        //        }
        //    }
        //}
        /// <summary>
        /// 根据指定数据库执行sql语句
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="dbParameter">参数</param>
        //public void ExecuteBySqlTrans(string sql, object dbParameter, IRepository db)
        //{
        //    try
        //    {
        //        if (dbParameter is JObject)
        //        {
        //            var paramter = SqlHelper.JObjectToParameter((JObject)dbParameter);
        //            if (db != null)
        //            {
        //                db.ExecuteBySql(sql, paramter);
        //            }
        //        }
        //        else if (dbParameter is List<FieldValueParam>)
        //        {
        //            var paramter = SqlHelper.FieldValueParamToParameter((List<FieldValueParam>)dbParameter);
        //            if (db != null)
        //            {
        //                db.ExecuteBySql(sql, paramter);
        //            }
        //        }
        //        else
        //        {
        //            if (db != null)
        //            {
        //                db.ExecuteBySql(sql, dbParameter);
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        if (ex is ExceptionEx)
        //        {
        //            throw;
        //        }
        //        else
        //        {
        //            throw ExceptionEx.ThrowServiceException(ex);
        //        }
        //    }
        //}
        #endregion

        #endregion
    }
}
